/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.github.pig.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.model.dto.DeptTree;
import com.github.pig.admin.model.dto.GroupTree;
import com.github.pig.admin.model.dto.RoleDTO;
import com.github.pig.admin.model.entity.SysDept;
import com.github.pig.admin.model.entity.SysGroup;
import com.github.pig.admin.model.entity.SysRole;
import com.github.pig.admin.model.entity.SysUser;
import com.github.pig.admin.service.SysGroupService;
import com.github.pig.admin.service.SysRoleMenuService;
import com.github.pig.admin.service.SysRoleService;
import com.github.pig.admin.service.SysUserService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.vo.UserVO;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/group")
public class GroupController extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysGroupService sysGroupService;

    /**
     * 通过ID查询分组
     *
     * @param id ID
     * @return 分组
     */
    @GetMapping("/{id}")
    public SysGroup group(@PathVariable Integer id) {
        EntityWrapper<SysGroup> nw = new EntityWrapper<>();
        nw.where("id = {0}", id);
        return sysGroupService.selectOne(nw);
    }

    /**
     * 添加分组
     *
     * @param sysGroup 分组信息
     * @return success、false
     */
    @PostMapping
    public R<Boolean> group(@RequestBody SysGroup sysGroup,UserVO userVO) {
        UserVO u = sysUserService.findUserByUsername(userVO.getUsername());
        sysGroup.setCreateUser(u.getUserId());
        sysGroup.setCreateTime(new Date());
        sysGroup.setDelFlag(Integer.valueOf(CommonConstant.STATUS_NORMAL));
        return new R<>(sysGroupService.insert(sysGroup));
    }

    /**
     * 修改分组
     *
     * @param sysGroup 分组信息
     * @return success/false
     */
    @PutMapping
    public R<Boolean> groupUpdate(@RequestBody SysGroup sysGroup, UserVO userVO) {
        UserVO u = sysUserService.findUserByUsername(userVO.getUsername());
        SysGroup s = sysGroupService.selectById(sysGroup.getId());
        s.setUpdateTime(new Date());
        s.setUpdateUser(u.getUserId());
        s.setName(sysGroup.getName());
        s.setSort(sysGroup.getSort());
        return new R<>(sysGroupService.updateAllColumnById(s));
    }

    @DeleteMapping("/{id}")
    public R<Boolean> groupDel(@PathVariable Integer id, UserVO userVO) {
        UserVO u = sysUserService.findUserByUsername(userVO.getUsername());
        SysGroup sysGroup = new SysGroup();
        sysGroup.setUpdateUser(u.getUserId());
        sysGroup.setUpdateTime(new Date());
        sysGroup.setId(id);
        sysGroup.setDelFlag(Integer.valueOf(CommonConstant.STATUS_DEL));
        return new R<>(sysGroupService.updateById(sysGroup));
    }

    /**
     *
     * @return
     */
    @GetMapping("/groupList")
    public List<SysGroup> groupList() {
        return sysGroupService.selectList(new EntityWrapper<>());
    }

    /**
     * 分页查询分组信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/groupPage")
    public Page groupPage(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, Integer.valueOf(CommonConstant.STATUS_NORMAL));
        return sysGroupService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/groupTree")
    public JSONArray getTree() {
        SysGroup condition = new SysGroup();
        condition.setDelFlag(Integer.valueOf(CommonConstant.STATUS_NORMAL));
        String treeStr = JSONArray.toJSONString(sysGroupService.selectListTree(new EntityWrapper<>(condition)));
        treeStr = treeStr.replaceAll("\"children\":\\[\\],", "");
        return JSONArray.parseArray(treeStr);
    }
}
